<?php
namespace app\controller\manager;

use app\BaseController;
use \think\facade\Db;
use think\Request;
use app\model\Academy;
use app\model\Major;
use app\model\Clase;

/**
 * 班级控制器
 * @todo 完善增删改查
 * @todo 完善excel导入
 */
class ClaseManage extends BaseController
{
    
    /**
     * 班级列表
     */
    public function list(){
        $majorID = $this->request->param('majorID','not null');
        $enterYear = $this->request->param('enterYear','not null');
        $claseList = Clase::field(['claseID','majorID','claseName','enterYear'])->where('majorID',$majorID)->where('enterYear',$enterYear)->select();
        return resultSuccess($claseList);
    }

    /**
     * 班级列表细节
     */
    public function listDetail(){
        $majorID = $this->request->param('majorID','not null');
        $enterYear = $this->request->param('enterYear','not null');
        $claseList = Clase::field(['claseID','clase.majorID'=>'majorID','claseName','majorName','enterYear'])->where('enterYear',$enterYear)->join('major','major.majorID = clase.majorID')->where('clase.majorID',$majorID)->paginate();
        return resultSuccess($claseList);
    }

    /**
     * 增加班级
     */
    public function addClase(Request $request)
    {
        if ($request->isPost()) {
            $clase = new Clase;
            if ($clase->save(
                [
                    'claseName' => $request->param('claseName'),
                    'majorID' => $request->param('majorID'),
                    'enterYear' =>$request->param('enterYear')
                ]
            )) {
                return resultSuccess();
            } else {
                return resultError();
            }
        } else {
            return resultError();
        }
    }

    /**
     * 删除班级
     */
    public function deleteClase(Request $request)
    {
        if ($request->isGet()) {
            $clase = Clase::find($request->param('claseID'));
            if ($clase->delete()) {
                return resultSuccess();
            } else {
                return resultError();
            }
        } else {
            return resultError();
        }
    }

    /**
     * 修改班级信息
     */
    public function editClase(Request $request)
    {
        if ($request->isPost()) {
            $clase = Clase::find($request->param('claseID'));
            $clase->claseName = $request->param('claseName');
            $clase->majorID = $request->param('majorID');
            $clase->enterYear = $request->param('enterYear');
            if ($clase->save()) {
                return resultSuccess();
            } else {
                return resultError();
            }
        } else {
            return resultError();
        }
    }
}
